//
//  MeHeaderView.m
//  CarLifes
//
//  Created by yu on 2017/6/22.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "MeHeaderView.h"
#import "UIImageView+LBBlurredImage.h"

#define kSingleLineMaxWidth 200

@implementation MeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        // 背景View
        _backgroundImageView = [[UIImageView alloc] init];
        [_backgroundImageView setImageToBlur:[UIImage imageNamed:@"expert_bg.jpg"] blurRadius:20 completionBlock:nil];
        _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
        _backgroundImageView.clipsToBounds = YES;
        _backgroundImageView.frame = CGRectMake(0, 0, kScreenWidth, 250);
        [self addSubview:_backgroundImageView];
        
        // 黑色遮罩
        _shelterView = [[UIView alloc] init];
        _shelterView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        [_backgroundImageView addSubview:_shelterView];
        
        _headerSubview = [[HeaderSubview alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, 250)];
        
        _shelterView.sd_layout
        .spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    }
    return self;
}

@end


@implementation HeaderSubview

- (void)updateWeatherWithInfo:(AMapLocalWeatherLive *)info
{
    _temperatureLabel.text = info.temperature;
    _positionLabel.text = info.city;
    _weatherLabel.text = info.weather;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        // 头像
        _iconView = [[UIButton alloc] init];
        [_iconView setImage:IMAGE_NAMED(@"expert_bg.jpg") forState:UIControlStateNormal];
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
        _iconView.layer.masksToBounds = YES;
//        _iconView.layer.cornerRadius = 35;
        _iconView.layer.borderWidth = 1.5;
        [_iconView addTarget:self action:@selector(iconClick) forControlEvents:UIControlEventTouchUpInside];
        _iconView.layer.borderColor = kLineGrayColor.CGColor;
        [self addSubview:_iconView];
        
        // 昵称
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"捷匹艾斯";
        _nameLabel.textColor = [UIColor whiteColor];
        [self addSubview:_nameLabel];
        
        // 温度
        _temperatureLabel = [[UILabel alloc] init];
        _temperatureLabel.font = SYSTEM_FONT(60);
        _temperatureLabel.text = @"--";
        _temperatureLabel.textColor = [UIColor whiteColor];
        [self addSubview:_temperatureLabel];
        
        // 温度℃
        _degreeView = [[UIImageView alloc] init];
        _degreeView.image = IMAGE_NAMED(@"degree");
        _degreeView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_degreeView];
        
        // 位置图片
        _positionView = [[UIImageView alloc] init];
        _positionView.image = IMAGE_NAMED(@"location_icon");
        _positionView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_positionView];
        
        // 位置信息
        _positionLabel = [[UILabel alloc] init];
        _positionLabel.text = @"济南市";
        _positionLabel.font = SYSTEM_FONT(16);
        _positionLabel.textColor = [UIColor whiteColor];
        [self addSubview:_positionLabel];
        
        // 天气
        _weatherLabel = [[UILabel alloc] init];
        _weatherLabel.textColor = [UIColor whiteColor];
        _weatherLabel.text = @"多云";
        _weatherLabel.font = SYSTEM_FONT(16);
        [self addSubview:_weatherLabel];
        
        // 设置
        _settingBtn = [[UIButton alloc] init];
        [_settingBtn setImage:IMAGE_NAMED(@"setting") forState:UIControlStateNormal];
        [_settingBtn addTarget:self action:@selector(setting:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_settingBtn];

#pragma mark - AutoLayout -   
        
        _iconView.sd_layout
        .rightSpaceToView(self, 12)
        .bottomSpaceToView(self, 0)
        .widthRatioToView(self, 0.2)
        .heightEqualToWidth();
//        _iconView.sd_cornerRadiusFromWidthRatio = @(0.5);
        
        [_nameLabel setSingleLineAutoResizeWithMaxWidth:kSingleLineMaxWidth];
        _nameLabel.sd_layout
        .rightSpaceToView(_iconView, 15)
        .centerYEqualToView(_iconView)
        .heightIs(20);
        
        [_temperatureLabel setSingleLineAutoResizeWithMaxWidth:kSingleLineMaxWidth];
        _temperatureLabel.sd_layout
        .leftSpaceToView(self, 20)
        .topSpaceToView(self, 40)
        .heightIs(60);
        
        _degreeView.sd_layout
        .topSpaceToView(self, 48)
        .leftSpaceToView(_temperatureLabel, 5)
        .widthIs(35)
        .autoHeightRatio(0.85);
        
        _positionView.sd_layout
        .leftEqualToView(_temperatureLabel)
        .topSpaceToView(_temperatureLabel, 40)
        .widthIs(15)
        .autoHeightRatio(1.3);
        
        [_positionLabel setSingleLineAutoResizeWithMaxWidth:kSingleLineMaxWidth];
        _positionLabel.sd_layout
        .leftSpaceToView(_positionView, 2)
        .centerYEqualToView(_positionView)
        .heightRatioToView(_positionView, 1);
        
        [_weatherLabel setSingleLineAutoResizeWithMaxWidth:kSingleLineMaxWidth];
        _weatherLabel.sd_layout
        .leftSpaceToView(_positionLabel, 10)
        .centerYEqualToView(_positionLabel)
        .heightRatioToView(_positionLabel, 1);
        
//        _settingBtn.sd_layout
//        .topSpaceToView(self, 5)
//        .rightSpaceToView(self, 20)
//        .widthIs(30)
//        .heightIs(30);
    }
    return self;
}

- (void)iconClick {
    if (_iconBlock) {
        _iconBlock();
    }
}

- (void)setting:(id)sender {
    if (_settingBlock) {
        _settingBlock();
    }
}

@end
